package home3;

import java.lang.reflect.Field;

public class home3 {

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {

        student student = new student();
             String s="propertyName";
              setProperty(student,s,"张三");
        System.out.println(getProperty(student, s));

    }
    public static void setProperty(Object obj, String propertyName, Object value) throws NoSuchFieldException, IllegalAccessException {
        Class<?> aClass = obj.getClass();
        Field df = aClass.getDeclaredField(propertyName);
        df.setAccessible(true);
        df.set(obj,value);

    }
    public static Object getProperty(Object obj, String propertyName) throws IllegalAccessException, NoSuchFieldException {
        Class<?> aClass = obj.getClass();
        Field declaredField = aClass.getDeclaredField(propertyName);
           declaredField.setAccessible(true);

        return (String) declaredField.get(obj);
    }
}
